/*     */ package com.zimbra.cs.filter.jsieve;
/*     */ 
/*     */ import com.zimbra.common.mime.InternetAddress;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.filter.ZimbraMailAdapter;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ import org.apache.jsieve.Argument;
/*     */ import org.apache.jsieve.Arguments;
/*     */ import org.apache.jsieve.ScriptCoordinate;
/*     */ import org.apache.jsieve.SieveContext;
/*     */ import org.apache.jsieve.StringListArgument;
/*     */ import org.apache.jsieve.TagArgument;
/*     */ import org.apache.jsieve.exception.SieveException;
/*     */ import org.apache.jsieve.mail.MailAdapter;
/*     */ import org.apache.jsieve.tests.AbstractTest;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class MeTest
/*     */   extends AbstractTest
/*     */ {
/*     */   private static final String IN = ":in";
/*     */   private String[] headers;
/*     */   
/*     */   protected void validateArguments(Arguments args, SieveContext ctx)
/*     */     throws SieveException
/*     */   {
/*  52 */     Iterator<Argument> itr = args.getArgumentList().iterator();
/*  53 */     if (itr.hasNext()) {
/*  54 */       Argument arg = (Argument)itr.next();
/*  55 */       if ((arg instanceof TagArgument)) {
/*  56 */         TagArgument tag = (TagArgument)arg;
/*  57 */         if (tag.is(":in")) {
/*  58 */           if (itr.hasNext()) {
/*  59 */             arg = (Argument)itr.next();
/*  60 */             if ((arg instanceof StringListArgument)) {
/*  61 */               this.headers = ((String)((StringListArgument)arg).getList().get(0)).split(",");
/*     */             } else {
/*  63 */               throw ctx.getCoordinate().syntaxException(":in is missing an argument");
/*     */             }
/*     */           } else {
/*  66 */             throw ctx.getCoordinate().syntaxException(":in is missing an argument");
/*     */           }
/*     */         } else {
/*  69 */           throw ctx.getCoordinate().syntaxException("Unknown tag: " + tag.getTag());
/*     */         }
/*     */       } else {
/*  72 */         throw ctx.getCoordinate().syntaxException("Unexpected argument: " + arg.getValue());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   protected boolean executeBasic(MailAdapter mail, Arguments args, SieveContext ctx) throws SieveException
/*     */   {
/*  79 */     assert (this.headers != null);
/*  80 */     if (!(mail instanceof ZimbraMailAdapter)) {
/*  81 */       return false;
/*     */     }
/*  83 */     Mailbox mbox = ((ZimbraMailAdapter)mail).getMailbox();
/*  84 */     List<InternetAddress> addrs = new ArrayList();
/*  85 */     for (String header : this.headers) {
/*  86 */       for (String value : mail.getHeader(header)) {
/*  87 */         List<InternetAddress> inetAddrs = InternetAddress.parseHeader(value);
/*  88 */         if (inetAddrs != null) {
/*  89 */           addrs.addAll(inetAddrs);
/*     */         }
/*     */       }
/*     */     }
/*     */     try {
/*  94 */       Account account = mbox.getAccount();
/*  95 */       me = AccountUtil.getEmailAddresses(account);
/*  96 */       me.addAll(AccountUtil.getImapPop3EmailAddresses(account));
/*  97 */       for (InternetAddress addr : addrs) {
/*  98 */         String email = addr.getAddress();
/*  99 */         if ((email != null) && (me.contains(email.toLowerCase())))
/* 100 */           return true;
/*     */       }
/*     */     } catch (ServiceException e) {
/*     */       Set<String> me;
/* 104 */       ZimbraLog.filter.error("Failed to lookup my addresses", e);
/*     */     }
/* 106 */     return false;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/filter/jsieve/MeTest.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */